//节点
class Node{
    constructor(element){
        this.element = element;
        this.next = null;
    }
}

//循环链表

class CircleList{
    constructor(){
        this.head = null;  //首节点
        this.count = 0;    //链表的个数
    }

    //找到指定节点
    getNode(index){   //0
        let current = this.head;
        for(let i = 0; i < index;i++){
            current = current.next;
        }
        return current
    }

    //在末尾天加
    push(element){
        //创建节点
        let node = new Node(element);
        if(this.head === null){
            //1.链表为空
            this.head = node;
            node.next = this.head;  //末尾节点的next指向第一个节点
        }else{
            //2.链表不为空
            let last = this.getNode(this.count - 1); //最后一个节点
            last.next = node;
            node.next = this.head;
        }
        this.count++;
    }
    //n 总人数  k：报数的上限
    ysfCircle(n,k){
        for(let i=1;i<=n;i++){
            this.push(i);
        }

        let helper = this.head;  //1
        let current = this.head;  //当前报数的节点  1

        while(helper.next !== this.head){
            helper = helper.next; //记录current的前一个节点 //30
        }

        //helper 30

        //只要人数大于15，接着报数
        while(this.count > 15){
            for(let i = 1;i<k;i++){ //9
                helper = helper.next;//8
                current = current.next;//9
            }
            //current当前要删除的节点
            helper.next = current.next;
            console.log(current.element);//要淘汰的人员
            current = current.next;
            this.count--;
        }
    }
}

let c =  new CircleList();

c.ysfCircle(30,9);

console.log(c)
// c.push('a');

// c.push('b');

// console.log(c);